home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / editors / emcs1857 / 1857bi~1.zoo / lisp / fill.el < prev    next >
Encoding:
Text File  |  1992-02-03  |  6.5 KB  |  202 lines

  1. ;; Fill commands for Emacs
  2. ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 1, or (at your option)
  9. ;; any later version.
  10.  
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ;; GNU General Public License for more details.
  15.  
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  18. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.  
  21. (defun set-fill-prefix ()
  22.   "Set the fill-prefix to the current line up to point.
  23. Filling expects lines to start with the fill prefix
  24. and reinserts the fill prefix in each resulting line."
  25.   (interactive)
  26.   (setq fill-prefix (buffer-substring
  27.              (save-excursion (beginning-of-line) (point))
  28.              (point)))
  29.   (if (equal fill-prefix "")
  30.       (setq fill-prefix nil))
  31.   (if fill-prefix
  32.       (message "fill-prefix: \"%s\"" fill-prefix)
  33.     (message "fill-prefix cancelled")))
  34.  
  35. (defun fill-region-as-paragraph (from to &optional justify-flag)
  36.   "Fill region as one paragraph: break lines to fit fill-column.
  37. Prefix arg means justify too.
  38. From program, pass args FROM, TO and JUSTIFY-FLAG."
  39.   (interactive "r\nP")
  40.   (save-restriction
  41.     (narrow-to-region from to)
  42.     (goto-char (point-min))
  43.     (skip-chars-forward "\n")
  44.     (narrow-to-region (point) (point-max))
  45.     (setq from (point))
  46.     (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
  47.              (regexp-quote fill-prefix))))
  48.       ;; Delete the fill prefix from every line except the first.
  49.       ;; The first line may not even have a fill prefix.
  50.       (and fpre
  51.        (progn
  52.          (if (>= (length fill-prefix) fill-column)
  53.          (error "fill-prefix too long for specified width"))
  54.          (goto-char (point-min))
  55.          (forward-line 1)
  56.          (while (not (eobp))
  57.            (if (looking-at fpre)
  58.            (delete-region (point) (match-end 0)))
  59.            (forward-line 1))
  60.          (goto-char (point-min))
  61.          (and (looking-at fpre) (forward-char (length fill-prefix)))
  62.          (setq from (point)))))
  63.     ;; from is now before the text to fill,
  64.     ;; but after any fill prefix on the first line.
  65.  
  66.     ;; Make sure sentences ending at end of line get an extra space.
  67.     (goto-char from)
  68.     (while (re-search-forward "[.?!][])""']*$" nil t)
  69.       (insert ? ))
  70.     ;; The change all newlines to spaces.
  71.     (subst-char-in-region from (point-max) ?\n ?\ )
  72.     ;; Flush excess spaces, except in the paragraph indentation.
  73.     (goto-char from)
  74.     (skip-chars-forward " \t")
  75.     (while (re-search-forward "   *" nil t)
  76.       (delete-region
  77.        (+ (match-beginning 0)
  78.       (if (save-excursion
  79.            (skip-chars-backward " ])\"'")
  80.            (memq (preceding-char) '(?. ?? ?!)))
  81.           2 1))
  82.        (match-end 0)))
  83.     (goto-char (point-max))
  84.     (delete-horizontal-space)
  85.     (insert "  ")
  86.     (goto-char (point-min))
  87.     (let ((prefixcol 0))
  88.       (while (not (eobp))
  89.     (move-to-column (1+ fill-column))
  90.     (if (eobp)
  91.         nil
  92.       (skip-chars-backward "^ \n")
  93.       (if (if (zerop prefixcol) (bolp) (>= prefixcol (current-column)))
  94.           (skip-chars-forward "^ \n")
  95.         (forward-char -1)))
  96.     (delete-horizontal-space)
  97.     (insert ?\n)
  98.     (and (not (eobp)) fill-prefix (not (equal fill-prefix ""))
  99.          (progn
  100.            (insert fill-prefix)
  101.            (setq prefixcol (current-column))))
  102.     (and justify-flag (not (eobp))
  103.          (progn
  104.            (forward-line -1)
  105.            (justify-current-line)
  106.            (forward-line 1)))))))
  107.  
  108. (defun fill-paragraph (arg)
  109.   "Fill paragraph at or after point.
  110. Prefix arg means justify as well."
  111.   (interactive "P")
  112.   (save-excursion
  113.     (forward-paragraph)
  114.     (or (bolp) (newline 1))
  115.     (let ((end (point)))
  116.       (backward-paragraph)
  117.       (fill-region-as-paragraph (point) end arg))))
  118.  
  119. (defun fill-region (from to &optional justify-flag)
  120.   "Fill each of the paragraphs in the region.
  121. Prefix arg (non-nil third arg, if called from program)
  122. means justify as well."
  123.   (interactive "r\nP")
  124.   (save-restriction
  125.    (narrow-to-region from to)
  126.    (goto-char (point-min))
  127.    (while (not (eobp))
  128.      (let ((initial (point))
  129.        (end (progn
  130.          (forward-paragraph 1) (point))))
  131.        (forward-paragraph -1)
  132.        (if (>= (point) initial)
  133.        (fill-region-as-paragraph (point) end justify-flag)
  134.      (goto-char end))))))
  135.  
  136. (defun justify-current-line ()
  137.   "Add spaces to line point is in, so it ends at fill-column."
  138.   (interactive)
  139.   (save-excursion
  140.    (save-restriction
  141.     (let (ncols beg)
  142.       (beginning-of-line)
  143.       (forward-char (length fill-prefix))
  144.       (skip-chars-forward " \t")
  145.       (setq beg (point))
  146.       (end-of-line)
  147.       (narrow-to-region beg (point))
  148.       (goto-char beg)
  149.       (while (re-search-forward "   *" nil t)
  150.     (delete-region
  151.      (+ (match-beginning 0)
  152.         (if (save-excursion
  153.          (skip-chars-backward " ])\"'")
  154.          (memq (preceding-char) '(?. ?? ?!)))
  155.         2 1))
  156.      (match-end 0)))
  157.       (goto-char beg)
  158.       (while (re-search-forward "[.?!][])""']*\n" nil t)
  159.     (forward-char -1)
  160.     (insert ? ))
  161.       (goto-char (point-max))
  162.       (setq ncols (- fill-column (current-column)))
  163.       (if (search-backward " " nil t)
  164.       (while (> ncols 0)
  165.         (let ((nmove (+ 3 (% (random) 3))))
  166.           (while (> nmove 0)
  167.         (or (search-backward " " nil t)
  168.             (progn
  169.              (goto-char (point-max))
  170.              (search-backward " ")))
  171.         (skip-chars-backward " ")
  172.         (setq nmove (1- nmove))))
  173.         (insert " ")
  174.         (skip-chars-backward " ")
  175.         (setq ncols (1- ncols))))))))
  176.  
  177. (defun fill-individual-paragraphs (min max &optional justifyp mailp)
  178.   "Fill each paragraph in region according to its individual fill prefix.
  179. Calling from a program, pass range to fill as first two arguments.
  180. Optional third and fourth arguments JUSTIFY-FLAG and MAIL-FLAG:
  181. JUSTIFY-FLAG to justify paragraphs (prefix arg),
  182. MAIL-FLAG for a mail message, i. e. don't fill header lines."
  183.   (interactive "r\nP")
  184.   (let (fill-prefix)
  185.     (save-restriction
  186.       (save-excursion
  187.     (narrow-to-region min max)
  188.     (goto-char (point-min))
  189.     (while (progn
  190.          (skip-chars-forward " \t\n")
  191.          (not (eobp)))
  192.       (setq fill-prefix (buffer-substring (point) (progn (beginning-of-line) (point))))
  193.       (let ((fin (save-excursion (forward-paragraph) (point)))
  194.         (start (point)))
  195.         (if mailp
  196.         (while (re-search-forward "^[ \t]*[^ \t\n]*:" fin t)
  197.           (forward-line 1)))
  198.         (cond ((= start (point))
  199.            (fill-region-as-paragraph (point) fin justifyp)
  200.            (goto-char fin)))))))))
  201.  
  202.